//
//  BWHomeTableViewController.swift
//  weibo
//
//  Created by libowen on 16/5/20.
//  Copyright © 2016年 libowen. All rights reserved.
//
import UIKit
import Foundation
import SVProgressHUD
class BWHomeTableViewController: BWBaseTableViewController {
    
    lazy var statues: [BWStatus] = [BWStatus]()
    
    
    override func viewDidLoad() {
         super.viewDidLoad()
        visitorLoginView.updateUI("关注一些人,回到这里看看有什么惊喜", imageName: "visitordiscover_feed_image_smallicon",isHome: true)
        if BWUserAccountModel.sharedAccountViewModel.userLogin == false {
            return
        }
        setNavBar()
        perpareForTableView()
        loadData()
    }

    private func setNavBar() {
            let popItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: "push")
            navigationItem.rightBarButtonItem = popItem
    }
    
    
    @objc private func push() {
        let detail = BWDetailViewController()
        detail.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(detail,animated: true)
        
    }
    
    private func perpareForTableView() {
        
        tableView.registerClass(BWStatuesCell.self, forCellReuseIdentifier: HomeCellId)
        tableView.rowHeight = 300
    }
    
}

extension BWHomeTableViewController {
    private func loadData() {
        BWNetworkTool.sharedTools.loadHomePageData { (reponseObject, error) -> () in
            if error != nil {
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                return
            }
            guard let dict = reponseObject as? [String : AnyObject] else  {
                return
            }
            guard let array = dict["statuses"] as? [[String : AnyObject]] else {
                return
            }
            var tempArrary = [BWStatus]()
            for item in array {
                let s = BWStatus(dict: item)
                tempArrary.append(s)
                
            }
            self.statues = tempArrary
            self.tableView.reloadData()
        }
    }
}
extension BWHomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statues.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! BWStatuesCell
        cell.status = statues[indexPath.row]
        return cell
    }
}




















